<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Requests\Admin\BaseRequest;
use App\Log\DnsLog;
use App\Models\Admin\AdminOperateModule;
use App\Org\Dns;
use Illuminate\Support\Facades\Validator;

class DnsController extends AdminController
{
    use AdminTrait;

    public function dnsList(BaseRequest $request)
    {
        $input=$request->all();
        //过滤数据
        $this->vali_rules['RRKeyWord']='string';
        $validator = Validator::make($input, $this->vali_rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $data_send=[
            'PageNumber' => $input['page'],
            'PageSize' => $input['size'],
        ];
        if(isset($input['RRKeyWord']))
        {
            $data_send['RRKeyWord']=$input['RRKeyWord'];
        }
        try {
            $res=Dns::getDomainRecords($data_send);
            $res['domain']=Dns::DomainName;
            $res['ip']=Dns::IP;
            return $this->success($res);
        }
        catch (\Exception $exception)
        {
            return $this->error(['-1',$exception->getMessage()]);
        }
    }

    public function addRecord(BaseRequest $request)
    {
        $input=$request->only('RR');
        $rules=[
          'RR'=>'required|string'
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        try {
            Dns::addDomainRecords($input);
            self::addLog(['RR'=>$input['RR']],[],1);
            return $this->success();
        }
        catch (\Exception $exception)
        {
            return $this->error(['-1',$exception->getMessage()]);
        }
    }

    public function editRecord(BaseRequest $request)
    {
        $input=$request->only('RR','RecordId');
        $rules=[
            'RR'=>'required|string',
            'RecordId'=>'required|string'
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        try {
            //获取原来的RR
            $res_old=Dns::DescribeDomainRecordInfo($input['RecordId']);
            Dns::UpdateDomainRecord($input);
            self::addLog(['RR'=>$res_old->RR],['RR'=>$input['RR']],2);
            return $this->success();
        }
        catch (\Exception $exception)
        {
            return $this->error(['-1',$exception->getMessage()]);
        }
    }

    public function deleteRecord(BaseRequest $request)
    {
        $input=$request->only('RecordId');
        $rules=[
            'RecordId'=>'required|string'
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        try {
            //获取原来的RR
            $res_old=Dns::DescribeDomainRecordInfo($input['RecordId']);
            Dns::deleteDomainRecord($input);
            self::addLog(['RR'=>$res_old->RR],[],3);
            return $this->success();
        }
        catch (\Exception $exception)
        {
            return $this->error(['-1',$exception->getMessage()]);
        }
    }

    public function editRemark(BaseRequest $request)
    {
        $input=$request->only('RecordId','Remark');
        $rules=[
            'RecordId'=>'required|string',
            'Remark'=>'string',
        ];
        $validator = Validator::make($input,$rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        try {
            //获取原来的RR
            $res_old=Dns::DescribeDomainRecordInfo($input['RecordId']);
            Dns::UpdateDomainRecordRemark($input);
            self::addLog(['Remark'=>$res_old->Remark?? '','RR'=>$res_old->RR],['Remark'=>$input['Remark']],2);
            return $this->success();
        }
        catch (\Exception $exception)
        {
            return $this->error(['-1',$exception->getMessage()]);
        }
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',1)->where('type',$type)->where('model','dns')->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new DnsLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }

}
